export const regexEnum = {
  phone: /^1[13456789]\d{9}$/,
  idcard: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/,
  intege1: /^[1-9]\d*$/, // 正整数
  intege: /^[0-9]\d*$/, // 正整数
  decmal4: /^[0-9]*\.[0-9]{0,4}$/, // 小数点保留后4位
  digital: /^\d*$/, // 数字
  chinese: /^[\u4e00-\u9fa5]+$/,
  bankCard: /^\d{14,20}$/,
  email: /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/,
  principal_person: /^.*[\u4e00-\u9fa5].*$/,
  english_phone: /^\d{10,11}$/,
  legalName: {
    reg: /^[\w\u4e00-\u9fa5-]{1,15}$/,
    message: '请使用15位以内的中英文或数字或-组合的字符'
  }
}

let validate = {}

validate.regexEnum = regexEnum

validate.validBussAuthNum = (rule, value, callback) => {
  const { length } = value
  if (length === 15 || length === 18) {
    callback()
  } else {
    callback(new Error('工商注册号必需为15位或者18位'))
  }
}
validate.validEmail = (rule, value, callback) => {
  if (!regexEnum.email.test(value)) {
    callback(new Error('请输入正确的邮箱地址'))
  } else {
    callback()
  }
}

validate.validPrincipalPerson = (rule, value, callback) => {
  if (!regexEnum.principal_person.test(value)) {
    callback(new Error('请输入正确的法人名称格式'))
  } else {
    callback()
  }
}

validate.validSerivicePhone = (rule, value, callback) => {
  if (regexEnum.intege.test(value) && value.length >= 5 && value.length <= 15) {
    callback()
  } else {
    callback(new Error('请输入正确的客服电话'))
  }
}

validate.validBankCard = (rule, value, callback) => {
  if (!regexEnum.bankCard.test(value)) {
    callback(new Error('请输入正确的银行卡号'))
  } else {
    callback()
  }
}

validate.validChinese = (rule, value, callback) => {
  if (!regexEnum.chinese.test(value)) {
    callback(new Error('请使用中文字符'))
  } else {
    callback()
  }
}

validate.validPhone = (rule, value, callback) => {
  if (!regexEnum.phone.test(value)) {
    callback(new Error('手机号格式不正确'))
  } else {
    callback()
  }
}

validate.validEnglishPhone = (rule, value, callback) => {
  if (!regexEnum.english_phone.test(value)) {
    callback(new Error('手机号格式不正确'))
  } else {
    callback()
  }
}

validate.validIDCard = (rule, value, callback) => {
  if (!regexEnum.idcard.test(value)) {
    callback(new Error('身份证格式不正确'))
  } else {
    callback()
  }
}

validate.validIntege1 = (rule, value, callback) => {
  if (value && !regexEnum.intege1.test(value)) {
    callback(new Error('格式不正确'))
  } else {
    callback()
  }
}

validate.validDecmal4 = (rule, value, callback) => {
  if (value && !regexEnum.decmal4.test(value)) {
    callback(new Error('格式不正确'))
  } else {
    callback()
  }
}

validate.validDigital = (rule, value, callback) => {
  if (value && !regexEnum.digital.test(value)) {
    callback(new Error('请输入数字'))
  } else {
    callback()
  }
}

validate.validFileObject = (rule, value, callback) => {
  if ((!value.id || !value.name)) {
    callback(new Error('请上传文件'))
  } else {
    callback()
  }
}

export default validate
